﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using LP580.CG;

public partial class GiftList : System.Web.UI.Page
{
    private int CurrentClassID = Utils.RQ_int("c");
    private int pageindex = Utils.RQ_int("pi");

    private int OrderType = Utils.RQ_int("od");
    private string SearchKey = Utils.RQ_str("sk");
    private int PriceSpanId = Utils.RQ_int("ps");
    private int AreaId = Utils.RQ_int("at");

    private int pagesize = 12;
    protected void Page_Load(object sender, EventArgs e)
    {

        int RootClassID = 0;
        if (CurrentClassID > 0)
        {
            clsGiftProductClass cls = new clsGiftProductClass(CurrentClassID);
            RootClassID = cls.GetRoot().GiftProductClassID;
            List<clsGiftProductClass> ls = cls.GetParentCollection();
            foreach (clsGiftProductClass c in ls)
            {
                if (c.GiftProductClassLevel != 2)
                    ltPath.Text += string.Format(" &gt;&gt; <a href='GiftList.aspx?c={0}'>{1}</a>", c.GiftProductClassID, c.GiftProductClassName);
            }
        }
        //品牌厂商
        bindCompany(RootClassID);

        //分类列表
        bindClassList(RootClassID);

        //筛选条件
        bindConditions();

        //产品列表
        bindGiftList(RootClassID);
    }

    private void bindGiftList(int RootClassID)
    {
        StringBuilder sbHtml = new StringBuilder();

        List<clsGiftProduct> ls = clsGiftProduct.GetCollection(CurrentClassID, OrderType, SearchKey, PriceSpanId, AreaId);

        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = ls;
        pds.PageSize = pagesize;
        pds.CurrentPageIndex = (pageindex - 1 < 0) ? 0 : (pageindex - 1);
        pds.AllowPaging = true;

        sbHtml.AppendLine("<table width='794' border='0' cellpadding='0' cellspacing='0' class='mtop10' id='TBcon'>");
        sbHtml.AppendLine("<tr>");
        int i = 0;
        foreach (clsGiftProduct p in pds)
        {
            i++;
            sbHtml.AppendLine("<td align='center' class='ptop8'>");
            sbHtml.AppendLine(string.Format("<a href='Gift.aspx?id={0}' target='_blank'><img src='{1}' width='185' height='185' class='proboder'></a>", p.GiftProductID,p.GiftProductPic));
            sbHtml.AppendLine("<table width='185' border='0' cellspacing='0' cellpadding='0'>");
            sbHtml.AppendLine(string.Format("<tr><td height='22' colspan='2' align='left'><a href='Gift.aspx?id={1}' target='_blank'>{0}</a></td></tr>", p.GiftProductName, p.GiftProductID)); //len:12
            sbHtml.AppendLine(string.Format("<tr><td height='22' colspan='2' align='left'>采购价：<span class='caigoujia'>￥{0}元</span></td></tr>", p.GiftProductPriceSpanName));
            sbHtml.AppendLine(string.Format("<tr><td height='22' colspan='2' align='left'><a href='Company.aspx?id={1}' target='_blank'>{0}</a></td></tr>", p.UserCompanyName, p.UserID)); //len:10
            sbHtml.AppendFormat("<tr><td width='99' align='left'><img src='images/zxkf.gif' width='87' height='28' s='{0}'></td>", p.UserID);
            sbHtml.AppendFormat("<td width='86' align='left'><a href='javascript:addFav(\"g\",{0})'><img src='images/sc.gif' width='50' height='28'></a></td></tr>", p.GiftProductID);
            sbHtml.AppendLine("</table>");
            sbHtml.AppendLine("</td>");
            if (i % 4 == 0)
                sbHtml.AppendLine("</tr><tr>");
        }
        sbHtml.AppendLine("</tr>");
        sbHtml.AppendLine("</table>");
        ltGiftList.Text = sbHtml.ToString();

        string baseUrl = "GiftList.aspx?c=" + CurrentClassID + "&od=" + OrderType + "&sk=" + SearchKey + "&ps=" + PriceSpanId + "&at=" + AreaId;

        ltPage.Text = LvEasyPager.CreateUser(baseUrl+"&", pageindex, pagesize, ls.Count);
    }

    private void bindConditions()
    {
        StringBuilder sbHtml = new StringBuilder();

        sbHtml.AppendLine("<table width='794' border='0' cellpadding='0' cellspacing='0' class='mtop10'>");
        sbHtml.AppendLine("<form name='searchform' id='searchform' action='?' method='get'>");
        sbHtml.AppendLine("<tr>");
        sbHtml.AppendLine("<td width='63' align='left' class='bottomline'>排列方式：</td>");
        
        string url = "GiftList.aspx?c=" + CurrentClassID + "&od=0&sk=" + SearchKey + "&ps=" + PriceSpanId + "&at=" + AreaId;
        sbHtml.AppendLine("<td width='67' align='left' class='bottomline'><a href='" + url + "'><img src='images/dj.gif' width='58' height='20'></a></td>");
        
        url = "GiftList.aspx?c=" + CurrentClassID + "&od=1&sk=" + SearchKey + "&ps=" + PriceSpanId + "&at=" + AreaId;
        sbHtml.AppendLine("<td width='62' align='left' class='bottomline'><a href='" + url + "'><img src='images/zx.gif' width='58' height='20'></a></td>");
        sbHtml.AppendLine("<td width='158' align='right' class='bottomline'>关键字：</td>");
        sbHtml.AppendLine("<td width='117' align='left' class='bottomline'><input name='c' id='c' type='hidden' value='" + CurrentClassID + "' /><input name='od' id='od' type='hidden' value='" + OrderType + "' /><input name='sk' id='sk' value='" + SearchKey + "' type='text' size='14'></td>");
        sbHtml.AppendLine("<td width='70' align='left' class='bottomline'><input type='submit' value='筛选'></td>");
        sbHtml.AppendLine("<td width='70' align='right' class='bottomline'>价格范围：</td>");
        sbHtml.AppendLine("<td width='87' align='right' class='bottomline'><select name='ps' id='ps' onchange='searchform.submit()'>");
        sbHtml.AppendLine("<option value='0'>选择价格</option>");
        List<clsGiftProductPriceSpan> ls = clsGiftProductPriceSpan.GetCollection();
        foreach (clsGiftProductPriceSpan ps in ls)
        {
            sbHtml.AppendLine(string.Format("<option value='{0}' {2}>{1}</option>", ps.GiftProductPriceSpanID, ps.GiftProductPriceSpanName, PriceSpanId == ps.GiftProductPriceSpanID ? "selected" : ""));
        }
        sbHtml.AppendLine("</select>");
        sbHtml.AppendLine("</td>");
        sbHtml.AppendLine("<td width='100' align='right' class='bottomline'><select name='at' id='at' onchange='searchform.submit()'>");
        sbHtml.AppendLine("<option value='0'>店铺所在地</option>");
        sbHtml.AppendLine("</select></td>");
        sbHtml.AppendLine("</tr>");
        sbHtml.AppendLine("</form>");
        sbHtml.AppendLine("</table>");

        ltConditions.Text = sbHtml.ToString();
    }

    private void bindClassList(int clsid)
    {
        StringBuilder sbHtml = new StringBuilder();
        List<clsGiftProductClass> ls = null;
        if (clsid == 0)
        {
            ls = new clsGiftProductClass().GetDirectChildCollection();
        }
        else
        {
            clsGiftProductClass cls = new clsGiftProductClass(clsid);
            ltClassListTitle.Text = cls.GiftProductClassName;
            //ltPath.Text = " &gt;&gt; " + cls.GiftProductClassName;
            ls = cls.GetChildCollection(2);
        }
        string url = "GiftList.aspx?c={0}&od="+OrderType+"&ps=" + PriceSpanId + "&at=" + AreaId;
        foreach (clsGiftProductClass c in ls)
        {
            sbHtml.AppendFormat("<a href='" + url + "'>{1}</a><span class='datetext'>({2})</span>", c.GiftProductClassID, c.GiftProductClassName, c.GiftProductCount);
        }
        ltClassList.Text = sbHtml.ToString();
    }

    private void bindCompany(int clsid)
    {
        StringBuilder sbHtml = new StringBuilder();
        sbHtml.AppendLine("<table width='100%' border='0' cellspacing='0' cellpadding='0'>");
        List<clsUserCompanyInfo> ls = getCompanyInfoList();
        sbHtml.AppendLine("<tr>");
        int i = 0;
        foreach (clsUserCompanyInfo ci in ls)
        {
            i++;
            sbHtml.AppendLine("<td height='52' align='left'><img src='/" + ci.CompanyLogo + "' width='69' height='42' alt='" + ci.CompanyName + "' class='logoboder'></td>");
            if (i % 2 == 0)
                sbHtml.AppendLine("</tr><tr>");
        }
        sbHtml.AppendLine("</tr>");
        sbHtml.AppendLine("</table>");
        ltCompanyList.Text = sbHtml.ToString();
    }

    private List<clsUserCompanyInfo> getCompanyInfoList()
    {
        List<clsUserCompanyInfo> ls = new List<clsUserCompanyInfo>();
        for (int i = 0; i < 20; i++)
        {
            clsUserCompanyInfo c = new clsUserCompanyInfo();
            c.CompanyName = "大力公司";
            ls.Add(c);
        }
        return ls;
    }
}